/*
    Todo controller that handles managing todos
*/
todoApp.controller("TodoController", function ($scope, TodoService, PopupService) {

    $scope.Todos = [];  // List of todos
    $scope.Todo = {TodoId: null, Description: null, Completed:false};  // A todo with defaults
    $scope.totalCompleted = 0;
    $scope.totalNotCompleted = 0;
    $scope.totalTodos = 0;
    $scope.filterOptions = {allTodos: true, completed: null};

    PopupService.blockUi();
    
    // Load todos
    $scope.init = function () {
        var promise = TodoService.getTodos();
        promise.then(function (todos) {
            $scope.Todos = todos;
            $scope.UpdateCount();
            PopupService.unBlockUi();
        },
            function (error) {
                PopupService.unBlockUi();
                PopupService.showError(error.data.Message);
            });
    };

    $scope.init();

    // save a todo
    $scope.saveTodo = function (Todo) {
        var promise = TodoService.saveTodo(Todo);
        promise.then(function (returnedTodo) {
            updateScope(returnedTodo);
        },
        function (error) {
            PopupService.showError(error.data.Message);
        });
    };

    // Delete Todo
    $scope.deleteTodo = function (todoId) {
        var promise = TodoService.deleteTodo(todoId);
        promise.then(function () {
            removeIfExists(todoId);
            $scope.UpdateCount();
            $scope.clearTodo();
        },
        function (error) {
            PopupService.showError(error.data.Message);
        });
    };
    
    // Filter completed
    $scope.filterByCompleted = function(completed) {
        $scope.filterOptions.allTodos = false;
        $scope.filterOptions.completed = completed;
    };
    
    // Filter show all
    $scope.filterByAll = function() {
        $scope.filterOptions.allTodos = true;
    };

    // Update the UI after save or update
    function updateScope(todo) {
        removeIfExists(todo.TodoId);
        $scope.Todos.push(todo);
        $scope.UpdateCount();
        $scope.clearTodo();
        PopupService.showSuccess('Todo is saved...');
    }

    function removeIfExists(todoId) {
        // Find the item and if found remove it
        var result = $.grep($scope.Todos, function (e) {
            return e.TodoId == todoId;
        });

        if (result.length == 1) {
            var todoToFind = result[0];
            $scope.Todos.splice($scope.Todos.indexOf(todoToFind), 1);
        }
    }

    // Select a todo in todo boxes for Update/Delete
    $scope.selectTodo = function (Todo) {
        $scope.Todo.TodoId = Todo.TodoId;
        $scope.Todo.Description = Todo.Description;
        $scope.Todo.Completed = Todo.Completed;
    };

    // Clear todo boxes
    $scope.clearTodo = function () {
        $scope.Todo.TodoId = null;
        $scope.Todo.Description = null;
        $scope.Todo.Completed = false;
    };

    // Flag to show or hide the detail table
    $scope.UpdateCount = function () {
        $scope.TodoCount = $scope.Todos.length;

        var totalCompleted = 0;
        var totalNotCompleted = 0;
        
        for (var ctr = 0; ctr < $scope.Todos.length; ctr++) {
            if ($scope.Todos[ctr].Completed == true)
                totalCompleted += 1;
            else {
                totalNotCompleted += 1;
            }
        }

        $scope.totalCompleted = totalCompleted;
        $scope.totalNotCompleted = totalNotCompleted;
        $scope.totalTodos = totalCompleted + totalNotCompleted;
    };
    
    // pagination
    $scope.currentPage = 0;
    $scope.pageSize = 5;
    $scope.numberOfPages = function () {
        return Math.ceil($scope.Todos.length / $scope.pageSize);
    };
    
});
